# File: tkSimpleDialog.py

from Tkinter import *
import os

class Dialog(Toplevel):

        def __init__(self, parent, title = None, view = None):

                Toplevel.__init__(self, parent)
                self.transient(parent)

                self.view = view

                if title:
                        self.title(title)

                self.parent = parent

                self.result = None

                body = Frame(self)
                self.initial_focus = self.Body(body)
                body.pack(padx=5, pady=5)

                self.Buttonbox()

                self.grab_set()

                if not self.initial_focus:
                        self.initial_focus = self

                self.protocol("WM_DELETE_WINDOW", self.Cancel)

                self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                        parent.winfo_rooty()+50))

                self.initial_focus.focus_set()

                self.wait_window(self)

        #
        # construction hooks

        def Body(self, master):
                # create dialog body.  return widget that should have
                # initial focus.  this method should be overridden

                pass

        def Buttonbox(self):
                # add standard button box. override if you don't want the
                # standard buttons
        
                box = Frame(self)

                w = Button(box, text="OK", width=10, command=self.Ok, default=ACTIVE)
                w.pack(side=LEFT, padx=5, pady=5)
                w = Button(box, text="Cancel", width=10, command=self.Cancel)
                w.pack(side=LEFT, padx=5, pady=5)

                self.bind("<Return>", self.Ok)
                self.bind("<Escape>", self.Cancel)

                box.pack()

        #
        # standard button semantics

        def Ok(self, event=None):

                if not self.Validate():
                        self.initial_focus.focus_set() # put focus back
                        return

                self.withdraw()
                self.update_idletasks()

                self.Apply()

                self.Cancel()

        def Cancel(self, event=None):

                # put focus back to the parent window
                self.parent.focus_set()
                self.destroy()

        #
        # command hooks

        def Validate(self):

                return 1 # override

        def Apply(self):

                pass # override
